package jim.org;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;
import javax.swing.border.EmptyBorder;

import net.miginfocom.swing.MigLayout;

import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;

import util.Attributes;

/**
 * @author Jonas Dietz and Ramon Victor from the Cologne University of Applied Sciences
 * @since 2012
 * This is the chatwindow frame.
 */
@SuppressWarnings("serial")
public class MessageWindows extends JFrame implements Runnable {

    private JPanel contentPane;
    private JTextArea conversationTextArea;
    private JButton btnSend;
    private JTextArea messageTextArea;
    private JScrollPane scrollPane;
    private JScrollPane scrollPane_1;
    private RosterEntry partner;
    private Chat chat;
    private String threadId;
    private MessageListener messageListener;

    /**
     * Create the frame.
     * @param chatManagerListener To create the proper chat.
     */
    public MessageWindows(RosterEntry entry, String id) {
	addWindowListener(new ThisWindowListener());
	partner = entry;
	threadId = id;

	setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
	setBounds(100, 100, 662, 458);

	contentPane = new JPanel();
	contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
	setContentPane(contentPane);
	contentPane.setBackground(Attributes.backgroundColor);
	contentPane.setLayout(new MigLayout("", "[541px][7px][70px]", "[345px][53px]"));

	scrollPane_1 = new JScrollPane();
	contentPane.add(scrollPane_1, "cell 0 0 3 1,grow");

	conversationTextArea = new JTextArea();
	scrollPane_1.setViewportView(conversationTextArea);
	conversationTextArea.setEditable(false);
	conversationTextArea.setBackground(Attributes.backgroundColor);
	conversationTextArea.setForeground(Attributes.textColor);

	btnSend = new JButton(Attributes.rs.getString("send"));
	btnSend.addActionListener(new BtnSendActionListener());
	btnSend.setBackground(Attributes.backgroundColor);
	btnSend.setForeground(Attributes.textColor);
	contentPane.add(btnSend, "cell 2 1,alignx right,aligny bottom");

	scrollPane = new JScrollPane();
	contentPane.add(scrollPane, "cell 0 1,grow");

	messageTextArea = new JTextArea();
	messageTextArea.setToolTipText(Attributes.rs.getString("sendToolTip"));
	messageTextArea.addKeyListener(new MessageTextAreaKeyListener());
	scrollPane.setViewportView(messageTextArea);
	messageTextArea.setBackground(Attributes.backgroundColor);
	messageTextArea.setForeground(Attributes.textColor);

	chat = ContactList.chatmanager.getThreadChat(threadId);
	messageListener = new MessageListener() {
	    @Override
	    public void processMessage(Chat tempChat, Message tempMessage) {
		if (!isVisible()) setVisible(true);
		if (tempMessage.getBody() != null) {
		    conversationTextArea.append(partner.getName() + ": " + tempMessage.getBody() + "\n");
		}
	    }
	}; 
	chat.addMessageListener(messageListener);
	setTitle(partner.getName());
	setVisible(true);
    }

    /**
     * Send the message to the chatpartner.
     * @throws XMPPException If J-IM has no connection to the server.
     */
    private void messageSend() throws XMPPException {
	chat.sendMessage(messageTextArea.getText());
	conversationTextArea.append(Attributes.rs.getString("me") + " " + messageTextArea.getText() + "\n");
	messageTextArea.setText("");
    }

    /**
     * Runs the frame.
     */
    @Override
    public void run() {
	try {
	    MessageWindows frame = new MessageWindows(partner, threadId);
	    frame.setVisible(true);
	    frame.setBackground(Attributes.backgroundColor);
	} catch (Exception e) {
	    e.printStackTrace();
	}
    }

    /**
     * Send button click.
     */
    private class BtnSendActionListener implements ActionListener {
	@Override
	public void actionPerformed(ActionEvent arg0) {
	    try {
		messageSend();
	    } catch (XMPPException e) {
		conversationTextArea.append(Attributes.rs.getString("cantSendMessage"));
	    }
	}
    }

    /**
     * Handles the SHIFT+ENTER send event.
     */
    private class MessageTextAreaKeyListener extends KeyAdapter {
	@Override
	public void keyPressed(KeyEvent arg0) {
	    if (arg0.getKeyCode()==KeyEvent.VK_ENTER && arg0.isShiftDown())
		try {
		    messageSend();
		} catch (XMPPException e) {
		    conversationTextArea.append(Attributes.rs.getString("cantSendMessage"));
		}
	}
    }

    /**
     * Simple close event.
     */
    private class ThisWindowListener extends WindowAdapter {
	@Override
	public void windowClosing(WindowEvent e) {
	    conversationTextArea.setText("");
	    setVisible(false);
	    ContactList.currentsConversations.remove(partner.getUser());
	}
    }
}
